package com.ruoyix.generator.util;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;

public class PomXmlUtils {
    private Document document;
    private File pomFile;

    public PomXmlUtils(File pomFile) {
        this.pomFile = pomFile;
        try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            document = documentBuilder.parse(pomFile);
        } catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
        }
    }

    public void addDependency(String groupId, String artifactId, String version) {
        Element dependenciesElement = findOrCreateDependenciesElement("dependencies");
        Element dependencyElement = createDependencyElement(groupId, artifactId, version);
        dependenciesElement.appendChild(dependencyElement);
    }

    public void addModule(String module) {
        Element dependenciesElement = findOrCreateDependenciesElement("modules");
        Element dependencyElement = createModuleElement(module);
        dependenciesElement.appendChild(dependencyElement);
    }



    public void saveChanges() {
        try {
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(pomFile);
            transformer.transform(source, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Element findOrCreateDependenciesElement(String parentTag) {
        Element root = document.getDocumentElement();
        NodeList dependenciesList = root.getElementsByTagName(parentTag);
        if (dependenciesList.getLength() > 0) {
            return (Element) dependenciesList.item(0);
        } else {
            Element dependencies = document.createElement(parentTag);
            root.appendChild(dependencies);
            return dependencies;
        }
    }

    private Element createDependencyElement(String groupId, String artifactId, String version) {
        Element dependency = document.createElement("dependency");
        createChildElement(dependency, "groupId", groupId);
        createChildElement(dependency, "artifactId", artifactId);
        createChildElement(dependency, "version", version);
        return dependency;
    }

    private Element createModuleElement(String module) {
        Element moduleTag = document.createElement("module");
        moduleTag.setTextContent(module);

        return moduleTag;
    }

    private void createChildElement(Element parent, String tagName, String textContent) {
        Element child = document.createElement(tagName);
        child.setTextContent(textContent);
        parent.appendChild(child);
    }
}
